package com.company.oa.announcement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.announcement.dto.FeedbackDTO;
import com.company.oa.announcement.entity.AnnouncementFeedback;
import com.company.oa.announcement.service.AnnouncementFeedbackService;
import com.company.oa.common.api.Result;
import com.company.oa.security.annotation.RequiresPermissions;
import com.company.oa.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/announcement-feedbacks")
public class AnnouncementFeedbackController {

    @Autowired
    private AnnouncementFeedbackService feedbackService;

    /**
     * 分页查询反馈列表
     */
    @GetMapping("/page/{announcementId}")
    @RequiresPermissions("announcement:feedback:list")
    public Result<IPage<AnnouncementFeedback>> getFeedbackPage(
            @PathVariable Long announcementId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        IPage<AnnouncementFeedback> pageResult = feedbackService.getFeedbackPage(page, size, announcementId);
        return Result.success(pageResult);
    }

    /**
     * 提交反馈
     */
    @PostMapping
    public Result<Boolean> submitFeedback(@RequestBody @Valid FeedbackDTO feedbackDTO) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        
        boolean result = feedbackService.submitFeedback(feedbackDTO, userId, userName);
        return Result.success(result);
    }

    /**
     * 获取反馈统计数据
     */
    @GetMapping("/stats/{announcementId}")
    @RequiresPermissions("announcement:feedback:stats")
    public Result<Map<String, Object>> getFeedbackStats(@PathVariable Long announcementId) {
        Map<String, Object> stats = feedbackService.getFeedbackStats(announcementId);
        return Result.success(stats);
    }

    /**
     * 检查用户是否已提交反馈
     */
    @GetMapping("/check/{announcementId}")
    public Result<Boolean> hasSubmittedFeedback(@PathVariable Long announcementId) {
        Long userId = SecurityUtils.getUserId();
        boolean hasSubmitted = feedbackService.hasSubmittedFeedback(announcementId, userId);
        return Result.success(hasSubmitted);
    }
}